#include <iostream>
#include <string>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace NS_tcpserver{
    class TcpServer
    {
      private:
        uint16_t port;
        int listen_sock;
      public:
        TcpServer(uint16_t _port): port(_port), listen_sock(-1)
        {}

        void InitTcpServer()
        {
            listen_sock = socket(AF_INET, SOCK_STREAM, 0);

            struct sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;

            
            if((bind(listen_sock, (sockaddr*)&local, sizeof(local))) < 0)
            {
                std::cerr << "bind error" << std::endl;
                exit(1);

            }  

            listen(listen_sock, 5);          
        }

        void Loop()
        {
            while(true)
            {
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);

                int sock = accept(listen_sock, (struct sockaddr*)&peer, &len);
                //获取连接后没有进行任何读写，更重要的是没有close(sock)
                std::cout << "get a new sock: " << sock << std::endl;

            }
        }
    };
}
